/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package jsr203.sun.nio.fs;

import jsr203.nio.file.attribute.UserPrincipal;
import static jsr203.sun.nio.fs.UnixNativeDispatcher.getgrgid;
import static jsr203.sun.nio.fs.UnixNativeDispatcher.getpwuid;

/**
 * Unix implementation of jsr203.nio.file.attribute.UserPrincipal
 */

class UnixUserPrincipal
    implements UserPrincipal
{
    private final int id;             // uid or gid
    private final boolean isGroup;
    private final String name;

    private UnixUserPrincipal(int id, boolean isGroup, String name) {
        this.id = id;
        this.isGroup = isGroup;
        this.name = name;
    }

    // return UserPrincipal representing given uid
    static UnixUserPrincipal fromUid(int uid) {
        String name = null;
        try {
            name = new String(getpwuid(uid));
        } catch (UnixException x) {
            name = Integer.toString(uid);
        }
        return new UnixUserPrincipal(uid, false, name);
    }

    // return UserPrincipal representing given uid and name
    static UnixUserPrincipal fromUid(int uid, String name) {
        return new UnixUserPrincipal(uid, false, name);
    }

    // return (group) UserPrincipal representing given gid
    static UnixUserPrincipal fromGid(int gid) {
        String name = null;
        try {
            name = new String(getgrgid(gid));
        } catch (UnixException x) {
            name = Integer.toString(gid);
        }
        return new UnixUserPrincipal(gid, true, name);
    }

    // return (group) UserPrincipal representing given gid and name
    static UnixUserPrincipal fromGid(int gid, String name) {
        return new UnixUserPrincipal(gid, true, name);
    }

    // return special UserPrincipal (OWNER@, GROUP@, ...)
    static UnixUserPrincipal createSpecial(String name) {
        return fromUid(-1, name);
    }

    int uid() {
        if (isGroup())
            throw new IllegalStateException();
        return id;
    }

    int gid() {
        if (!isGroup())
            throw new IllegalStateException();
        return id;
    }

    boolean isSpecial() {
        return id == -1;
    }

    @Override
    public boolean isGroup() {
        return isGroup;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(id);
        sb.append('=');
        sb.append(name);
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (!(obj instanceof UnixUserPrincipal))
            return false;
        UnixUserPrincipal other = (UnixUserPrincipal)obj;
        if ((this.id != other.id) ||
            (this.isGroup != other.isGroup)) {
            return false;
        }
        // specials
        if (this.id == -1 && other.id == -1)
            return this.name.equals(other.name);

        return true;
    }

    @Override
    public int hashCode() {
        return (id != -1) ? id : name.hashCode();
    }
}
